package com.zxl.grocery.modular.crawler.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxl.grocery.core.base.controller.BaseController;
import com.zxl.grocery.core.common.annotion.BussinessLog;
import com.zxl.grocery.core.common.annotion.Permission;
import com.zxl.grocery.core.common.constant.Const;
import com.zxl.grocery.core.common.constant.dictmap.DeleteDict;
import com.zxl.grocery.core.common.constant.dictmap.NovelDict;
import com.zxl.grocery.core.common.constant.factory.PageFactory;
import com.zxl.grocery.core.common.exception.BizExceptionEnum;
import com.zxl.grocery.core.common.page.PageInfoBT;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.kernel.model.exception.ServiceException;
import com.zxl.grocery.modular.crawler.entity.CrawlerNovel;
import com.zxl.grocery.modular.crawler.mapper.CrawlerNovelMapper;
import com.zxl.grocery.modular.crawler.util.StringUtils;
import com.zxl.grocery.modular.crawler.warpper.CrawlerMgrWarpper;
import com.zxl.grocery.modular.crawler.warpper.NovelMgrWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 小说管理控制器
 *
 * @author zxl
 * @Date 2017-08-11 09:55:27
 */
@Controller
@RequestMapping("/novelMgr")
public class NovelMgrController extends BaseController {

    private String PREFIX = "/crawler/novelMgr/";

    @Resource
    private CrawlerNovelMapper crawlerNovelMapper;

    /**
     * 跳转到小说管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "novelMgr.html";
    }

    /**
     * 跳转到添加小说管理
     */
    @RequestMapping("/novelMgr_add")
    public String novelMgrAdd() {
        return PREFIX + "novelMgr_add.html";
    }

    /**
     * 跳转到修改小说管理
     */
    @RequestMapping("/novelMgr_update")
    public String novelMgrUpdate(@RequestParam Integer novelMgrId) {
        if (ToolUtil.isEmpty(novelMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
//        CrawlerNovel crawlerNovel = crawlerNovelMapper.selectById(novelMgrId);
//        model.addAttribute("crawlerNovel", crawlerNovel);
//        LogObjectHolder.me().set(crawlerNovel);
        return PREFIX + "novelMgr_edit.html";
    }

    /**
     * 获取小说管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String snovelName,
                       @RequestParam(required = false) String snovelAuthor,
                       @RequestParam(required = false) String schapter,
                       @RequestParam(required = false) Integer sstatus,
                       @RequestParam(required = false) Integer splacedTop,
                       @RequestParam(required = false) Integer srecommend,
                       @RequestParam(required = false) Integer ssourcesId) {
        Page<CrawlerNovel> page = new PageFactory().defaultPage();
        List<Map<String, Object>> result = crawlerNovelMapper.getAllNovelList(page, beginTime, endTime, snovelName, snovelAuthor,
                schapter, sstatus, splacedTop, srecommend, ssourcesId);
        page.setRecords((List<CrawlerNovel>) new NovelMgrWarpper(result).wrap());
        return new PageInfoBT<>(page);
    }

    /**
     * 新增小说管理
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Permission(Const.ADMIN_NAME)
    @BussinessLog(value = "新增小说", key = "title,chapter", dict = NovelDict.class)
    @ResponseBody
    public Object add(CrawlerNovel crawlerNovel) {
        crawlerNovel.setCreateDate(new Date());
        crawlerNovel.setAuthor(ShiroKit.getUser().getName());
        crawlerNovel.setChapterSort(StringUtils.getChapterSort(crawlerNovel.getChapter()));
        crawlerNovel.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除小说管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除小说", key = "title,chapter,novelMgrId", dict = DeleteDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer novelMgrId) {
        if (ToolUtil.isEmpty(novelMgrId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.crawlerNovelMapper.deleteById(novelMgrId);
        return SUCCESS_TIP;
    }


    /**
     * 修改小说管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改小说", key = "typeId,title,chapter,content,sort,link,novelist,sourcesId,placedTop,status,recommend", dict = NovelDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(CrawlerNovel crawlerNovel) {
        if (ToolUtil.isEmpty(crawlerNovel) || crawlerNovel.getId() == null) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        int id = crawlerNovel.getId();
        CrawlerNovel temp = crawlerNovelMapper.selectById(id);
        crawlerNovel.setAuthor(ShiroKit.getUser().getName());
        crawlerNovel.setId(temp.getId());
        crawlerNovel.setModifyDate(new Date());
        crawlerNovel.setThumbUp(temp.getThumbUp());
        crawlerNovel.setChapterSort(StringUtils.getChapterSort(crawlerNovel.getChapter()));
        crawlerNovelMapper.updateById(crawlerNovel);
        return SUCCESS_TIP;
    }

    /**
     * 小说管理详情
     */
    @RequestMapping(value = "/detail/{id}")
    @ResponseBody
    public Object detail(@PathVariable Integer id) {
        CrawlerNovel crawlerNovel = crawlerNovelMapper.selectById(id);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(crawlerNovel);
        return super.warpObject((new CrawlerMgrWarpper(stringObjectMap)));
    }
}
